const api = require("../../../../ws_micro_shop/utils/api.js");
const tools = require("../../../../ws_micro_shop/utils/getuserinfo.js");
const app = getApp();
const { module: moduleName } = app.siteInfo;
const util = app.util;

let timer = null;

Page({
  data: {
    page_img: {},
    goods_list: null,
    rest_time_str: "--:--:--",
    id: "",
    hb_records: [],
    page: 1,
    showMore: false,
    abnorData: {
      imgSrc: "/common/static/images/abnor-06.png",
      text: "商家还未设置活动规则哟~"
    },
    abnorData1: {
      imgSrc: "/common/static/images/abnor-04.png",
      text: "暂无领取记录"
    },
    unit: ""
  },

  showHistory() {
    app.util.navigateTo("/ws_micro_shop_seller/pages/fxhb/list/list");
  },

  onLoad(opts) {
    let options = opts || {};
    let _this = this;
    let id = options.id ? options.id : "";
    this.setData({
      orgPrice: app.ws_micro_shop_globalData.orgPrice
    });
    app.tabbarindex = options.tabbarindex
      ? options.tabbarindex - 0
      : app.tabbarindex;
    app.pageOnLoad(_this);
    if (options && options.shopid) {
      app.ws_micro_shop_globalData.shopid = options.shopid;
    }
    let userinfo = wx.getStorageSync("user_info");
    app.loadCurrencyUnit().then(res => {
      _this.setData({
        unit: res.unit
      });
    });
    if (userinfo) {
      Number(userinfo.info) === 0 && tools.onGetInfo(this, userinfo.auth_mode);
      wx.showLoading({
        title: "加载中",
        mask: true
      });
      this.fxhbDetail(id);
      this.getHbRecords();
      util.prepareBindInfo(options, moduleName, _this);
    } else {
      app.ws_micro_shoplogin().then(res => {
        userinfo = wx.getStorageSync("user_info");
        Number(userinfo.info) === 0 &&
          tools.onGetInfo(_this, userinfo.auth_mode);
        wx.showLoading({
          title: "加载中",
          mask: true
        });
        _this.fxhbDetail(id);
        _this.getHbRecords();
        util.prepareBindInfo(options, moduleName, _this);
      });
    }
    let imageLine = api.default.index.substr(
      0,
      api.default.index.indexOf("/index.php")
    );
    _this.setData({
      page_img: {
        bg: imageLine + "/statics/images/fxhb/bg.png",
        close: imageLine + "/statics/images/fxhb/close.png",
        hongbao_bg: imageLine + "/statics/images/fxhb/hongbao_bg.png",
        open_hongbao_btn:
          imageLine + "/statics/images/fxhb/open_hongbao_btn.png",
        wechat: imageLine + "/statics/images/fxhb/wechat.png",
        coupon: imageLine + "/statics/images/fxhb/coupon.png",
        pointer_r: imageLine + "/statics/images/fxhb/pointer_r.png",
        best_icon: imageLine + "/statics/images/fxhb/best_icon.png",
        more_l: imageLine + "/statics/images/fxhb/more_l.png",
        more_r: imageLine + "/statics/images/fxhb/more_r.png",
        cry: imageLine + "/statics/images/fxhb/cry.png",
        share_modal_bg: imageLine + "/statics/images/fxhb/share_modal_bg.png"
      },
      opts: options,
      id
    });
  },

  fxhbDetail(id) {
    let _this = this;
    app.ws_micro_shoprequest({
      url: api.fxhb.detail,
      data: { id },
      success: function(res) {
        wx.hideLoading();
        if (res.code === 1) {
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false,
            success: function(e) {
              if (e.confirm) {
                Number(res.game_open) === 1
                  ? app.util.navigateTo(
                      "/ws_micro_shop_seller/pages/fxhb/open/open",
                      {},
                      true
                    )
                  : app.util.navigateTo(
                      "/ws_micro_shop_seller/pages/index/index",
                      {},
                      true
                    );
              }
            }
          });
          return;
        }
        if (res.code === 0) {
          let {
            rule,
            share_pic,
            share_title,
            coupon_total_money,
            rest_user_num,
            rest_time,
            hongbao,
            hongbao_list,
            is_my_hongbao,
            my_coupon,
            goods_list
          } = res.data;
          _this.setData({
            rule,
            share_pic,
            share_title,
            coupon_total_money,
            rest_user_num,
            rest_time,
            hongbao,
            hongbao_list,
            is_my_hongbao,
            my_coupon,
            goods_list
          });
          _this.setRestTimeStr();
        }
        _this.showShareModal();
      }
    });
  },

  getHbRecords() {
    let { page, hb_records } = this.data;
    app.ws_micro_shoprequest({
      url: api.fxhb.hb_records,
      data: {
        page
      },
      success: res => {
        hb_records = hb_records || [];
        let records = res.data || [];
        hb_records = hb_records.concat(records);
        this.setData({
          hb_records
        });
        if (hb_records.length < res.totalCount) {
          this.setData({
            showMore: true,
            showMoreText: "点击加载更多"
          });
        }
      }
    });
  },

  showMore() {
    let page = this.data.page;
    page++;
    this.setData({
      page,
      showMoreText: "加载中..."
    });
    this.getHbRecords();
  },

  showRule() {
    this.setData({
      showRule: true
    });
  },

  closeRule() {
    this.setData({
      showRule: false
    });
  },

  showShareModal() {
    this.setData({
      showShareModal: true
    });
  },

  closeShareModal() {
    this.setData({
      showShareModal: false
    });
  },

  setRestTimeStr() {
    let _this = this;
    let rest_time = _this.data.rest_time || false;
    if (!rest_time) {
      return;
    }
    rest_time = parseInt(rest_time, 10);
    if (rest_time <= 0) {
      _this.setData({
        rest_time_str: "00:00:00"
      });
      return;
    }
    timer && clearInterval(timer);
    timer = setInterval(function() {
      rest_time = _this.data.rest_time;
      if (rest_time <= 0) {
        timer && clearInterval(timer);
        _this.setData({
          rest_time_str: "00:00:00"
        });
        return;
      }
      let h = parseInt(rest_time / 3600, 10);
      let m = parseInt((rest_time % 3600) / 60, 10);
      let s = parseInt((rest_time % 3600) % 60, 10);
      _this.setData({
        rest_time: rest_time - 1,
        rest_time_str:
          (h < 10 ? "0" + h : h) +
          ":" +
          (m < 10 ? "0" + m : m) +
          ":" +
          (s < 10 ? "0" + s : s)
      });
    }, 1000);
  },

  detailSubmit(e) {
    let _this = this;
    wx.showLoading({
      mask: true
    });
    app.ws_micro_shoprequest({
      url: api.fxhb.detail_submit,
      method: "POST",
      data: {
        id: _this.data.hongbao.id,
        form_id: e.detail.formId
      },
      success: function(res) {
        if (res.code === 1) {
          wx.hideLoading();
          wx.showToast({
            title: res.msg || "",
            complete: function() {
              Number(res.game_open) === 0 &&
                app.util.navigateTo(
                  "/ws_micro_shop/pages/index/index",
                  {},
                  true
                );
            },
            icon: "none",
            duration: 3000
          });
          return;
        }
        if (res.code === 0) {
          wx.hideLoading();
          wx.showToast({
            title: res.msg,
            complete: function() {
              if (Number(res.reload) === 1) {
                wx.redirectTo({
                  url:
                    "/ws_micro_shop_seller/pages/fxhb/detail/detail?id=" +
                    _this.options.id
                });
              }
            },
            icon: "none",
            duration: 3000
          });
        }
      }
    });
  },

  onShareAppMessage() {
    let { id, share_title, share_pic } = this.data;
    let path =
      "/ws_micro_shop_seller/pages/fxhb/detail/detail?tabbarindex=" +
      app.tabbarindex +
      "&id=" +
      id +
      "&shopid=" +
      app.ws_micro_shop_globalData.shopid;
    path = util.spliceSharePath(path, "4", moduleName);
    this.closeShareModal();
    return {
      path,
      title: share_title || null,
      imageUrl: share_pic || null
      // complete: function () {
      //   that.closeShareModal();
      // },
    };
  },
  onShareTimeline() {
    let { id, share_title, share_pic } = this.data;
    return {
      title: share_title || '',
      imageUrl: share_pic || '',
      query: "tabbarindex=" +
      app.tabbarindex +
      "&id=" +
      id +
      "&shopid=" +
      app.ws_micro_shop_globalData.shopid
    }
  },

  onHide() {
    util.sendBindInfo(this.data);
  },

  onUnload() {
    util.sendBindInfo(this.data);
  },

  onNavigate: e => {
    let param = e.currentTarget.dataset;
    app.ws_micro_shop_navigate(param);
  }
});
